package com.yc.bean1;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

/**
 * @author zwei
 * @create 2024-07-28 14:45
 */
public class test1 {
    public static void main(String[] args) {
        //1.以前的方案 所有的对象是硬编码
        Address address=new Address("湖南","衡阳","珠晖区");
        Student student=new Student("张三",1,address);
        System.out.println(student);

        //2.使用spring框架来管理对象生命周期
        ApplicationContext context=new ClassPathXmlApplicationContext("beans.xml");
        Address address1= (Address) context.getBean("addr");
        System.out.println(address1);
        Address address2=context.getBean("addr",Address.class);
        System.out.println(address2);
        //默认情况下  spring创建对象是单例的
        System.out.println(address1.hashCode()+"\t"+address2.hashCode());


        //第三种方案     会重新调用构造方法 创建实例
        Resource resource=new ClassPathResource("beans.xml");   //读取了classpath下的beans.xml文件
        BeanFactory factory=new XmlBeanFactory(resource);
        Address address3= (Address) factory.getBean("addr");
        System.out.println(address3);
    }
}
